/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved.							  */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib.  */
/*----------------------------------------------------------------------------*/
#ifndef ANALOG_MODULE_H_
#define ANALOG_MODULE_H_

#include "ChipObject.h"
#include "Module.h"

class AnalogChannel;

/**
 * Analog Module class.
 * Each module can independently sample its channels at a configurable rate.
 * There is a 64-bit hardware accumulator associated with channel 1 on each module.
 * The accumulator is attached to the output of the oversample and average engine so that the center
 * value can be specified in higher resolution resulting in less error.
 */
class AnalogModule: public Module
{ 
public:
	static const long kTimebase = 40000000; ///< 40 MHz clock
	static const long kDefaultOversampleBits = 0;
	static const long kDefaultAverageBits = 0;
	static const float kDefaultSampleRate = 50000.0;
	static const unsigned kAccumulatorChannel = 1;

	void SetSampleRate(float samplesPerSecond);
	float GetSampleRate(void);
	void SetAverageBits(unsigned channel, unsigned bits);
	unsigned GetAverageBits(unsigned channel);
	void SetOversampleBits(unsigned channel, unsigned bits);
	unsigned GetOversampleBits(unsigned channel);
	signed short GetValue(unsigned channel);
	signed int GetAverageValue(unsigned channel);
	float GetAverageVoltage(unsigned channel);
	float GetVoltage(unsigned channel);
	unsigned int GetLSBWeight(unsigned channel);
	signed int GetOffset(unsigned channel);
	signed int VoltsToValue(int channel, float voltage);
   
	void InitAccumulator(void);
	void SetAccumulatorInitialValue(long long value);
	void ResetAccumulator(void);
	void SetAccumulatorCenter( int center );
	signed long long GetAccumulatorValue( void );
	unsigned int GetAccumulatorCount(void);
	void GetAccumulatorOutput(long long *value, unsigned *count);

	static unsigned SlotToIndex(unsigned slot);
	static AnalogModule* GetInstance(unsigned slot);

protected:
	AnalogModule(unsigned slot);
	virtual ~AnalogModule();

private:
	unsigned GetNumActiveChannels(void);
	unsigned GetNumChannelsToActivate(void);
	void SetNumChannelsToActivate(unsigned channels);

	tAI *m_module;
	bool m_sampleRateSet;
	long long m_accumulatorOffset;
	unsigned m_numChannelsToActivate;
};

#endif
